package com.libre.workflow.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.libre.common.base.BaseController;
import com.libre.common.utils.ResultUtil;
import com.libre.common.vo.PageVo;
import com.libre.common.vo.Result;
import com.libre.workflow.service.MyModelService;
import com.libre.workflow.vo.MyModelVo;
import lombok.extern.slf4j.Slf4j;
import org.flowable.ui.modeler.model.ModelRepresentation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 庞留杰
 * @version V1.0
 * @description 模型管理 创建/删除/发布
 * @date 2020/9/11 18:01
 **/
@Slf4j
@RestController
@RequestMapping("/workflow/model")
public class MyModelController extends BaseController {

    @Autowired
    MyModelService modelService;

    /**
     * 功能描述: 查询所有流程模型
     *
     * @param pageVo
     * @throws
     * @author: 庞留杰
     * @date: 2020/9/10 10:25
     */
    @GetMapping(value = "/findPage", produces = "application/json")
    public Result findPage(@ModelAttribute PageVo pageVo, @ModelAttribute MyModelVo modelVo) {
        Page page = modelService.findPage(pageVo, modelVo);
        return new ResultUtil<>().setData(page);
    }

    /**
     * 功能描述: 新建一个流程模型
     * 参数参考：flowable-ui-modeler-logic-6.5.0.jar->org.flowable.ui.modeler.model.ModelRepresentation
     *
     * @param modelRepresentation 包含字段：key:模型;Key name:模型名称;description:模型描述信息;modelType:0
     * @throws
     * @author: 庞留杰
     * @date: 2020/9/10 10:25
     */
    @PostMapping(value = "/createModel", produces = "application/json")
    public Result createModel(@RequestBody ModelRepresentation modelRepresentation) {
        ModelRepresentation model = modelService.createModel(modelRepresentation);
        return new ResultUtil<>().setData(model);
    }


    /**
     * @param modelId: 流程ID
     * @description 发布流程图
     * @author 庞留杰
     * @date 2020/9/11 17:59
     * @version V1.0
     **/
    @PostMapping("/deploy/{id}")
    public Object deploy(@PathVariable("id") String id) throws Exception {
        modelService.deploy(id);
        return new ResultUtil<>().setSuccessMsg("发布成功");
    }

    /**
     * 功能描述: 删除流程模型Model
     *
     * @param id （ModelId）
     * @throws
     * @author: 庞留杰
     * @date: 2020/9/10 10:26
     */
    @PostMapping("delete/{id}")
    public Result delete(@PathVariable("id") String id) {
        modelService.delete(id);
        return new ResultUtil<>().setSuccessMsg("删除成功");
    }


}
